//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "Issuer.h"
using namespace Cephei::QL::Experimental::Credit;
#include <gen/QL/Experimental/Credit/DefaultEvent.h>
#include <gen/QL/Experimental/Credit/DefaultProbKey.h>
#include <gen/QL/Termstructures/DefaultProbabilityTermStructure.h>
#include <gen/QL/Currency.h>
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Credit::CIssuer::CIssuer (boost::shared_ptr<QuantLib::Issuer>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phIssuer = NULL;
#endif
	_ppIssuer = &childNative;
    
}
Cephei::QL::Experimental::Credit::CIssuer::CIssuer (QuantLib::Issuer& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phIssuer = NULL;
#endif
	_ppIssuer = new boost::shared_ptr<QuantLib::Issuer> (&childNative);
    
    _IssuerOwner = owner;
    
}

Cephei::QL::Experimental::Credit::CIssuer::CIssuer (CIssuer^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phIssuer = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppIssuer = new boost::shared_ptr<QuantLib::Issuer> (copy->GetShared());
        
    }
}
Cephei::QL::Experimental::Credit::CIssuer::CIssuer (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phIssuer = NULL;
#endif
	if (!t->IsSubclassOf(CIssuer::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Credit::CIssuer::CIssuer (QuantLib::Handle<QuantLib::Issuer>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phIssuer = &childNative;
	_ppIssuer = &static_cast<boost::shared_ptr<QuantLib::Issuer>>(childNative.currentLink());
    
    _IssuerOwner = owner;
}
Cephei::QL::Experimental::Credit::CIssuer::CIssuer (QuantLib::Handle<QuantLib::Issuer> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phIssuer = &childNative;
	_ppIssuer = &static_cast<boost::shared_ptr<QuantLib::Issuer>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Credit::CIssuer::CIssuer (QuantLib::Issuer childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phIssuer = NULL;
#endif
	_ppIssuer = new boost::shared_ptr<QuantLib::Issuer> (new QuantLib::Issuer (childNative));
    
}
#endif

Cephei::QL::Experimental::Credit::CIssuer::~CIssuer ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppIssuer != NULL)
    {
	    delete _ppIssuer;
        _ppIssuer = NULL;
    }
}
Cephei::QL::Experimental::Credit::CIssuer::!CIssuer ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppIssuer != NULL)
    {
	    delete _ppIssuer;
    }
}
QuantLib::Issuer& Cephei::QL::Experimental::Credit::CIssuer::GetReference ()
{
    if (_ppIssuer == NULL) throw gcnew NativeNullException ();
	return **_ppIssuer;
}
boost::shared_ptr<QuantLib::Issuer>& Cephei::QL::Experimental::Credit::CIssuer::GetShared ()
{
    if (_ppIssuer == NULL) throw gcnew NativeNullException ();
	return *_ppIssuer;
}
QuantLib::Issuer* Cephei::QL::Experimental::Credit::CIssuer::GetPointer ()
{
    if (_ppIssuer == NULL) throw gcnew NativeNullException ();
	return &**_ppIssuer;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::Issuer>& Cephei::QL::Experimental::Credit::CIssuer::GetHandle ()
{
	if (_phIssuer == NULL)
	{
		_phIssuer = new Handle<QuantLib::Issuer> (*_ppIssuer);
	}
	return *_phIssuer;
}
#endif
bool Cephei::QL::Experimental::Credit::CIssuer::HasNative () 
{
	return (_ppIssuer != NULL);
}

Cephei::QL::Experimental::Credit::IDefaultEvent^ Cephei::QL::Experimental::Credit::CIssuer::DefaultedBetween (DateTime start, DateTime end, Cephei::QL::Experimental::Credit::IDefaultProbKey^ key, Microsoft::FSharp::Core::FSharpOption<Boolean>^ includeRefDate)
{
    CDefaultProbKey^ _Ckey;
    try
    {
        QuantLib::Date _start = (QuantLib::Date)ValueHelper::Convert (start);
        QuantLib::Date _end = (QuantLib::Date)ValueHelper::Convert (end);
        _Ckey = safe_cast<CDefaultProbKey^> (key);
        _Ckey->Lock();
        QuantLib::DefaultProbKey& _key = static_cast<QuantLib::DefaultProbKey&> (_Ckey->GetReference ()); 
        bool _includeRefDate = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (includeRefDate) ? (bool)ValueHelper::Convert (includeRefDate->Value) : false); //9a
    	boost::shared_ptr<QuantLib::DefaultEvent> _rv = (boost::shared_ptr<QuantLib::DefaultEvent>)(*_ppIssuer)->defaultedBetween ( _start,  _end,  _key,  _includeRefDate );   
        Cephei::QL::Experimental::Credit::CDefaultEvent^ _nrv = gcnew Cephei::QL::Experimental::Credit::CDefaultEvent (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ckey != nullptr) _Ckey->Unlock();
    }
}
Cephei::QL::Termstructures::IDefaultProbabilityTermStructure^ Cephei::QL::Experimental::Credit::CIssuer::DefaultProbability (Cephei::QL::Experimental::Credit::IDefaultProbKey^ key)
{
    CDefaultProbKey^ _Ckey;
    try
    {
        _Ckey = safe_cast<CDefaultProbKey^> (key);
        _Ckey->Lock();
        QuantLib::DefaultProbKey& _key = static_cast<QuantLib::DefaultProbKey&> (_Ckey->GetReference ()); 
    	Handle<QuantLib::DefaultProbabilityTermStructure>& _rv = (Handle<QuantLib::DefaultProbabilityTermStructure>&)(*_ppIssuer)->defaultProbability ( _key );   
        Cephei::QL::Termstructures::CDefaultProbabilityTermStructure^ _nrv = gcnew Cephei::QL::Termstructures::CDefaultProbabilityTermStructure (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ckey != nullptr) _Ckey->Unlock();
    }
}
Cephei::IVector<Cephei::QL::Experimental::Credit::IDefaultEvent^>^ Cephei::QL::Experimental::Credit::CIssuer::DefaultsBetween (DateTime start, DateTime end, Cephei::QL::Experimental::Credit::IDefaultProbKey^ contractKey, Boolean includeRefDate)
{
    CDefaultProbKey^ _CcontractKey;
    try
    {
        QuantLib::Date _start = (QuantLib::Date)ValueHelper::Convert (start);
        QuantLib::Date _end = (QuantLib::Date)ValueHelper::Convert (end);
        _CcontractKey = safe_cast<CDefaultProbKey^> (contractKey);
        _CcontractKey->Lock();
        QuantLib::DefaultProbKey& _contractKey = static_cast<QuantLib::DefaultProbKey&> (_CcontractKey->GetReference ()); 
        bool _includeRefDate = (bool)ValueHelper::Convert (includeRefDate);
    	std::vector<boost::shared_ptr<QuantLib::DefaultEvent> > _rv = (std::vector<boost::shared_ptr<QuantLib::DefaultEvent> >)(*_ppIssuer)->defaultsBetween ( _start,  _end,  _contractKey,  _includeRefDate );   
        CoVector<Cephei::QL::Experimental::Credit::IDefaultEvent^>^ _nrv = gcnew CoVector<Cephei::QL::Experimental::Credit::IDefaultEvent^>(gcnew CDefaultEventVector (_rv, this));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CcontractKey != nullptr) _CcontractKey->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

